//
//  CCFindTableViewCell.m
//  CwinCar
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCFindTableViewCell.h"
#import "CCFindHeadCollectionViewCell.h"
#import "BaseHeader.h"
#import "CCFinderViewController.h"
@implementation FindTableViewCell

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.image = [[UIImageView alloc] initWithFrame:CGRectMake(10 * suitableScreen_Width ,10 * suitableScreen_Width, 20 * suitableScreen_Width, 20 * suitableScreen_Width)];
        [self.contentView addSubview:self.image];
        
        self.title = [[UILabel alloc] initWithFrame:CGRectMake(40 * suitableScreen_Width, 10 * suitableScreen_Width, 120 * suitableScreen_Width, 20 * suitableScreen_Width)];
        [self.title setFont:[UIFont systemFontOfSize:14.0 * suitableScreen_Width]];
        [self.contentView addSubview:self.title];
        
        self.autoresizesSubviews = NO;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(mainScreen_Width / 6, 60 * suitableScreen_Width);
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, mainScreen_Width, 130 * suitableScreen_Width) collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
       
        [self.collectionView setBackgroundColor:[UIColor whiteColor]];
        
        [self.collectionView registerClass:[CCFindHeadCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self.collectionView registerClass:[CCFindHeadCollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self.contentView addSubview:self.collectionView];
   
    }
    return self;
}


- (void)getInfo:(CCFindModel *)model
{
    [self.image sd_setImageWithURL:[NSURL URLWithString:model.logo]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    [self.title setText:model.name];
    if ([model.cid isEqual: @0]) {
        
        // 隐藏其他的  防重影
        self.collectionView.hidden = NO;
        self.image.hidden = YES;
        self.title.hidden = YES;
        
        [self getAfn:self.collectionView];
    }
    else
    {
        self.image.hidden = NO;
        self.title.hidden = NO;
        self.collectionView.hidden = YES;
    }
}
#pragma mark 在cell里做数据解析

- (void)getAfn:(UICollectionView *)collectionView
{
    self.mArr = [[NSMutableArray alloc] init];
    [AFNblock getdataWithString:@"http://app.api.autohome.com.cn/autov4.8.8/dealer/hotbrands-pm1.json" block:^(id block) {
        
        for (NSDictionary *dic in [[block objectForKey:@"result"] objectForKey:@"list"]) {
            CCFindModel *model = [CCFindModel modelWithDictionary:dic];
            [self.mArr addObject:model];
           
        }
        
        [collectionView reloadData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
     return _mArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CCFindHeadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (_mArr.count != 0) {
        
        
        CCFindModel *model = [_mArr objectAtIndex:indexPath.row];
        
        
        [cell getFind:model];
        
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CCFindModel * model = [_mArr objectAtIndex:indexPath.row];
    
    
    [self.delegate giveUrl:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.8.8/cars/seriesprice-pm1-b%@-t1.json", model.cid]];
    
    CCFinderViewController * find = [[CCFinderViewController alloc]init];
    [find.carsTableView setFrame:CGRectMake(mainScreen_Width, 0, mainScreen_Width, mainScree_Height)];
    find.carsTableView.hidden = YES ;
    
    [find.coTableView setFrame:CGRectMake(0, 147 * suitableScreen_Width, mainScreen_Width, mainScree_Height)];
    find.coTableView.hidden = NO ;
    [find.coTableView reloadData];
    
    
}


@end
